 // export const URL = 'https://www.shlxkj.info:44313/API/api'
 export const URL = 'https://yun.shlxkj.info:44330/API/api'
// export const URL = 'http://192.168.3.118:8080/api'
// export const URL = 'http://192.168.3.12:8090/api'
// export const URL = 'https://zxyy.yncdc.cn/port5000/api'
import uuid from '@/js_sdk/com-ls-utils/components/ls-utils/index.js';
import store from '@/store'

const params = (url, data) => {
	url += '?'
	const keys = Object.keys(data)
	for (const key of keys) {
		url += `${key}=${encodeURIComponent(typeof data[key] === 'string' ? data[key].trim() : data[key])}&`
	}
	return url.substring(0, url.length - 1)
}

const request = ({
	method,
	url,
	data = {}
}) => {
	return new Promise((resolve, reject) => {
		console.log('url: ', params(url, data))
		const SerialNo = uuid.createUuid()
		uni.request({
			url: method === 'GET' ? params(url, data) : url,
			method,
			data,
			header: {
				Authorization: store.state.vx_token,
				SerialNo: SerialNo,
			},
			success: (res) => {
				// console.log('测试接口',res);
				if (res.statusCode === 200) {
					if(res.data.code == 502){
						uni.showToast({
							icon:'none',
							title: '身份验证失败，请重新登录',
						})
						store.commit('$uStore', {
							name: 'vx_token',
							value: ''
						})
						uni.navigateTo({
							url: '/pages/login/index'
						})
					}
					resolve(res.data ? res.data : res)
				} else if (res.statusCode === 401) {
					store.commit('$uStore', {
						name: 'vx_token',
						value: ''
					})
					let pages = getCurrentPages()
					let current = pages[pages.length - 1]
					if (current.route === 'pages/login/index' || current.route === 'pages/home/index') {
						return resolve(res.data)
					}
					uni.showToast({
						title: '需重新登录'
					})
					uni.navigateTo({
						url: '/pages/login/index'
					})
				} else {
					uni.showToast({
						title: res.data.msg,
						icon: 'error'
					})
					reject(res.data)
				}
				const data = res.data

				if (data) {
					resolve(data)
				} else {
					reject(res)
				}
			},
			fail: (err) => {
				if (err.statusCode === 401) {
					uni.redirectTo({
						url: '/pages/login/index'
					})
				}
				reject(err)
			}
		})
	})
}

// 登录
export function login(data) {
	return request({
		method: 'POST',
		url: `${URL}/Admin/Login`,
		data
	})
}
// 获取管理员信息
export function FindLoginUser() {
	return request({
		method: 'POST',
		url: `${URL}/Admin/FindLoginUser`
	})
}

// 获取省市区
export function ConvertCoordinate(data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/ConvertCoordinate`,
		data
	})
}


// 库存管理
// 分页查询入库记录
export function GetOutboundList(data) {
	return request({
		method: 'POST',
		url: `${URL}/DeviceOutbound/GetPagedDeviceOutboundsAsync`,
		data
	})
}

// 删除设备
export function DeleteDevice(data) {
	return request({
		method: 'POST',
		url: `${URL}/DeviceOutbound/DeleteAsync?deviceList=${data}`,
	})
}

// 新增入库记录
export function addDevice(data) {
	return request({
		method: 'POST',
		url: `${URL}/DeviceOutbound/AddDeviceOutbound`,
		data
	})
}
// 获取大箱码设备编号
export function getDevice(data) {
	return request({
		method: 'POST',
		url: `${URL}/LiveCode/GetDevicesByLiveCodeIdAndCount`,
		data
	})
}

// 获取当前账号关联的项目
export function getActivityList() {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/GetActivityByUserId`
	})
}

// 查看已签字的知情同意书
export function lookAgree(id) {
	return request({
		method: 'GET',
		url: `${URL}/SeedPopulationMsg/DownloadWordWithSignatureBase64?seedPopulationId=${id}`
	})
}
// 商业性查看已签字的知情同意书
export function lookAgree_Volunteer(id) {
	return request({
		method: 'GET',
		url: `${URL}/Prostitutemsg/DownloadWordWithSignatureBase64?seedPopulationId=${id}`
	})
}

// 获取志愿者列表
export function getSeedList(data) {
	return request({
		method: 'POST',
		url: `${URL}/SeedPopulationMsg/GetPaged`,
		data
	})
}
// 商业性志愿者列表
export function getSeedList_Volunteer(data) {
	return request({
		method: 'POST',
		url: `${URL}/Prostitutemsg/GetPaged`,
		data
	})
}

// 新增志愿者
export function addSeed(data) {
	return request({
		method: 'POST',
		url: `${URL}/SeedPopulationMsg/Create`,
		data
	})
}
// 商业性新增志愿者
export function addSeed_Volunteer(data) {
	return request({
		method: 'POST',
		url: `${URL}/Prostitutemsg/Create`,
		data
	})
}
// 修改志愿者
export function updateSeed(data) {
	return request({
		method: 'POST',
		url: `${URL}/SeedPopulationMsg/Update`,
		data
	})
}
// 商业性修改志愿者
export function updateSeed_Volunteer(data) {
	return request({
		method: 'POST',
		url: `${URL}/Prostitutemsg/Update`,
		data
	})
}
// 获取性伴检测列表
export function getSexList(data) {
	return request({
		method: 'POST',
		url: `${URL}/SexFriends/GetPaged`,
		data
	})
}
// 获取商业性性伴检测列表
export function getSexList_Whoremaster(data) {
	return request({
		method: 'POST',
		url: `${URL}/Whoremaster/GetPaged`,
		data
	})
}

// 修改性伴检测信息
export function updateSex(data) {
	return request({
		method: 'POST',
		url: `${URL}/SexFriends/Update`,
		data
	})
}
// 修改商业性性伴检测信息
export function updateSex_Whoremaster(data) {
	return request({
		method: 'POST',
		url: `${URL}/Whoremaster/Update`,
		data
	})
}

// 创建项目
export function createApplication(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/CreateActivityapplication`,
		data
	})
}

// 查询管理员下级用户
export function getSubUser() {
	return request({
		method: 'POST',
		url: `${URL}/Admin/FindSubordinateAdmins`,
	})
}

// 查询管理员上级用户
export function getSupUser() {
	return request({
		method: 'POST',
		url: `${URL}/Admin/FindSuperiorAdmins`,
	})
}

// 项目分页查询
export function GetApplicationList(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/PagingFindActivityapplication`,
		data
	})
}

// 修改项目
export function updateApplication(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/UpdateActivityapplication`,
		data
	})
}

// 作废项目
export function CancelActivityapplication(id) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/CancelActivityapplication?id=${id}`,
	})
}

// 获取首页数据
export function getHomePage(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetHomePageData`,
		data
	})
}

// 初筛列表
export function getResultList(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultList`,
		data
	})
}

//查询是否存在关联
export function GetReleted(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultInfo`,
		data
	})
}

// 创建关联关系以及检测记录
export function CreateReleted(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/CreateReagentResult`,
		data
	})
}

// 获取检测结果
export function Getresult(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultInfo`,
		data
	})
}

// 解密mac地址
export function DecryptMac(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptMac`,
		data
	})
}

// 解密
export function AESDecrypt(data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESDecrypt`,
		data
	})
}

// 加密
export function AESEncrypt(data) {
	return request({
		method: 'GET',
		url: `${URL}/WeChat/AESEncrypt`,
		data
	})
}


//9006解密
export function aesDecrypt2(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptYuzhi`,
		data
	})
}

//9005解密结果值
export function aesDecryptjieguozhi(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptJieguozhi`,
		data
	})
}

//获取接口阈值
export function aesDecryptGetAPiyuzhi(data) {
	return request({
		method: 'GET',
		url: `${URL}/Devicenoyuzhi/getYuzhiBy`,
		data
	})
}
//二次上报（结果）
export function UpReagentResult(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/UpReagentResult`,
		data
	})
}
// 创建自评表
export function CreateQuestionnaire(data) {
	return request({
		method: 'POST',
		url: `${URL}/Questionnaire/CreateQuestionnaire`,
		data
	})
}
// 根据手机号查询是否填过自评表
export function GetQuestionnaire(phone) {
	return request({
		method: 'POST',
		url: `${URL}/Questionnaire/GetQuestionnaire?PhoneNumber=${phone}`
	})
}











// 账号联络
export function AddTask(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/ActivityIssue`,
		data
	})
}



//创建项目总结记录
export function CreateActivityreport(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityreport/CreateActivityreport`,
		data
	})
}


//统计项目数量
export function GetActivityapplicationCount(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/GetActivityapplicationCount`,
		data
	})
}

//获取货物使用统计
export function GetUsageStatistics(data) {
	return request({
		method: 'POST',
		url: `${URL}/ActivityGoodsUsage/GetUsageStatistics`,
		data
	})
}

//结果值回传率  
export function CalculateReturnRate(data) {
	return request({
		method: 'POST',
		url: `${URL}/ActivityGoodsUsage/CalculateReturnRate`,
		data
	})
}


// 获取未读消息列表
export function GetNoteList(id) {
	return request({
		method: 'POST',
		url: `${URL}/Activitynotice/GetActivitynoticeInfo?userid=${id}`,
	})
}

// 获取已读列表
export function GetViewedNoteList(id) {
	return request({
		method: 'POST',
		url: `${URL}/Activitynotice/GetViewedActivitynoticeInfo?userid=${id}`,
	})
}

// 将通知改为已读
export function MarkAsViewed(id) {
	return request({
		method: 'POST',
		url: `${URL}/Activitynotice/MarkAsViewed?activitynoticeid=${id}`,
	})
}

// 申领试剂
export function ApplyReagent(data) {
	return request({
		method: 'POST',
		url: `${URL}/ActivityGoodsUsage/ApplyReagent`,
		data
	})
}

// 分配试剂
export function UpdateReagent(a,b,c) {
	return request({
		method: 'POST',
		url: `${URL}/ActivityGoodsUsage/UpdateDeviceStateByBatch?sourceCodeBatch=${a}&activityid=${b}&userid=${c}`
	})
}

// 退回试剂
export function ReturnReagent(data,id) {
	return request({
		method: 'POST',
		url: `${URL}/ActivityGoodsUsage/ReturnDeviceStateByBatch?sourceCodeBatch=${data}&userid=${id}`
	})
}

// 查询项目申请详情
export function GetActivityapplicationInfo(data) {
	return request({
		method: 'POST',
		url: `${URL}/Activityapplication/GetActivityapplicationInfo`,
		data
	})
}

// 根据项目id查询已分配的设备编号
export function getDeviceNosByActivityId(id) {
	return request({
		method: 'GET',
		url: `${URL}/Macinbound/getDeviceNosByActivityId?activityId=${id}`
	})
}



export function returnUp(data){
	return request({
		method: 'POST',
		url: `${URL}/Macoutbound/ReturnMacoutbounds`,
		data
	})
}

// 查询项目中未使用的试剂
export function getReturnDevice(id){
	return request({
		method: 'GET',
		url: `${URL}/Macinbound/GetReturnDeviceNosByActivityId?activityId=${id}`
	})
}


// 初筛结果
export function GetPrimaryList(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultList`,
		data
	})
}

export function GetDictionaryByType(data) {
	return request({
		method: 'GET',
		url: `${URL}/Dictionary/GetDictionaryByType`,
		data
	})
}



//获取数据字典详情
export function GetDictionaryList(data) {
	return request({
		method: 'GET',
		url: `${URL}/Dictionary/GetDictionaryList`,
		data
	})
}


// 获取手机号码
export function getPhone(data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/decryptData`,
		data
	})
}

// 注册
export function register(data) {
	return request({
		method: 'POST',
		url: `${URL}/Wechat/Login`,
		data
	})
}
// 验证设备是否存在库中
export function FindByUser(device) {
	return request({
		method: 'POST',
		url: `${URL}/SeedPopulationMsg/FindByUser?deviceNum=${device}`
	})
}
// 验证当前用户是否具有此设备的出库权限
export function VerifyPermissions(device) {
	return request({
		method: 'GET',
		url: `${URL}/DeviceOutbound/VerifyPermissions?deviceNo=${device}`
	})
}
// 批量验证当前用户是否具有此设备的出库权限
export function BatchVerifyPermissions(data) {
	return request({
		method: 'POST',
		url: `${URL}/DeviceOutbound/BatchVerifyPermissions`,
		data
	})
}


// 解密
// export function AESDecrypt(data) {
// 	return request({
// 		method: 'GET',
// 		url: `${URL}/WeChat/AESDecrypt`,
// 		data
// 	})
// }

// 加密
// export function AESEncrypt(data) {
// 	return request({
// 		method: 'GET',
// 		url: `${URL}/WeChat/AESEncrypt`,
// 		data
// 	})
// }

// 初次上报（用户信息）
export function CreateReagentResult(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/CreateReagentResult`,
		data
	})
}
//二次上报（结果）
// export function UpReagentResult(data) {
// 	return request({
// 		method: 'POST',
// 		url: `${URL}/ReagentResult/UpReagentResult`,
// 		data
// 	})
// }

// 援助
export function UpdateReagentResult(data) {
	return request({
		method: 'POST',
		url: `${URL}/ReagentResult/UpdateReagentResult`,
		data
	})
}



// 扫码领取电子检测盒
export function Shipment(data) {
	return request({
		method: 'GET',
		url: `${URL}/SelfService/Shipment`,
		data
	})
}

// 获取疾控中心
export function GetCDCVicinity(data) {
	return request({
		method: 'GET',
		url: `${URL}/CDC/GetCDCVicinity`,
		data
	})
}

// 报告过程数据
export function ReportedData(data) {
	return request({
		method: 'POST',
		url: `${URL}/WeChat/ReportedData`,
		data
	})
}
//Base64解码
export function Base64(data) {
	return request({
		method: 'GET',
		url: `${URL}/Decrypt/decryptMac`,
		data
	})
}

//查询是否存在关联
// export function GetReleted(data) {
// 	return request({
// 		method: 'GET',
// 		url: `${URL}/MacPhone/GetReleted`,
// 		data
// 	})
// }

//新建手机号与设备号关联
// export function CreateReleted(data) {
// 	return request({
// 		method: 'POST',
// 		url: `${URL}/MacPhone/CreateReleted`,
// 		data
// 	})
// }

//9005解密结果值
// export function aesDecryptjieguozhi(data) {
// 	return request({
// 		method: 'GET',
// 		url: `${URL}/Decrypt/decryptJieguozhi`,
// 		data
// 	})
// }


// 获取详情
export function GetDetail(data) {
	return request({
		method: 'GET',
		url: `${URL}/ReagentResult/GetReagentResultInfo`,
		data
	})
}



//出库
export function Outbound(data) {
	return request({
		method: 'PUT',
		url: `${URL}/Macoutbound/CreateMacoutbounds`,
		data
	})
}

// 查询当前账号需要出库的数据
export function FindAccountList(Id) {
	return request({
		method: 'GET',
		url: `${URL}/Macoutbound/FindAccountMac?accountId=${Id}`,
	})
}

// 新增意愿查询
export function addIntention(data) {
	return request({
		method: 'POST',
		url: `${URL}/UserOpinion/insertUserOpinion`,
		data
	})
}



// 创建VCT门诊个人咨询检测登记记录
export function createVCTOpinion(data) {
	return request({
		method: 'POST',
		url: `${URL}/Voluntaryreg/CreateVoluntaryreg`,
		data
	})
}

// 添加打卡记录
export function createPositioning(data) {
	return request({
		method: 'POST',
		url: `${URL}/PositioningPunch/InsertActivityPositioningPunch`,
		data
	})
}
// 领取设备
export function CreateClaimRecord(data) {
	return request({
		method: 'POST',
		url: `${URL}/ClaimRecord/CreateClaimRecord`,
		data
	})
}

//获取所有的标签名称
export function GetLabelsForAdminAsync(data) {
	return request({
		method: 'GET',
		url: `${URL}/DeviceLabel/GetLabelsForAdminAsync`,
		data
	})
}


// 获取南昌问卷信息
export function NanCangGetQuestionnaireAsync(data) {
	return request({
		method: 'POST',
		url: `${URL}/NcgaImport/GetQuestionnaireAsync?type=${data}`,
	})
}

